package mi.payment.ui.print.helper;

import android.graphics.Bitmap;
import android.text.TextUtils;

import java.math.BigDecimal;
import java.util.List;

import mi.payment.R;
import mi.payment.admin.pay88.IPay88Operation;
import woyou.aidlservice.jiuiv5.ICallback;

/**
 * 结算打印单
 */
public class PrintSettlementTicket extends BasePrint {

    public PrintSettlementTicket() {
        super();
    }

    /**
     * 结算总计单
     */
    public void printTotalSettlement(TotalSettlementDetail settlementDetail, final ICallback callback) throws Exception {

    }

    /**
     * 银行卡结算总计单
     */
    public void printBankTotalSettlement(TotalSettlementDetail settlementDetail, final ICallback callback) throws Exception {

    }

    /**
     * 二维码结算总计单
     */
    public void printCodeTotalSettlement(TotalSettlementDetail settlementDetail, final ICallback callback) throws Exception {
        printerService.enterPrinterBuffer(true);
        printLine();

        printTitle(settlementDetail);

        printSeparateLine();
        printerService.setFontSize(18, null);
        String[] colsTextArr = {
                getString(R.string.receipt_settlement_type),
                getString(R.string.receipt_settlement_pens),
                getString(R.string.receipt_settlement_amount)
        };
        int[] colsWidthArr = {13, 13, 13};
        int[] colsAlign = {0, 1, 1};
        printerService.printColumnsString(colsTextArr, colsWidthArr, colsAlign, null);

        printQRCode(settlementDetail);

        printFooter(callback, 18);

        printerService.lineWrap(4, callback);
        printerService.exitPrinterBufferWithCallback(true, callback);
    }

    // 打印二维码数据
    private void printQRCode(TotalSettlementDetail settlementDetail) throws Exception {
        printerService.setFontSize(20, null);
        List<SumTotal> list = settlementDetail.sumTotalsQRCode;
        if (list != null && list.size() > 0) {
            getCodeList(list);
        }
    }

    private void getCodeList(List<SumTotal> list) throws Exception {
        String lastTemp = null;
        for (SumTotal sumTotal : list) {
            boolean equals = TextUtils.equals("0", sumTotal.transTimes);
            if (equals) continue;

            String temp;
            String type;
            switch (sumTotal.platform) {
                case "234":
                    temp = "AliPay";
                    type = "Merchant";
                    break;
                case "233":
                    temp = "AliPay";
                    type = "User";
                    break;

                case "296":
                    temp = "WeChatPay-CNY";
                    type = "Merchant";
                    break;
                case "308":
                    temp = "WeChatPay-CNY";
                    type = "User";
                    break;

                case "343":
                    temp = "WeChatPay-MYR";
                    type = "Merchant";
                    break;
                case "344":
                    temp = "WeChatPay-MYR";
                    type = "User";
                    break;

                case "320":
                    temp = "Boost";
                    type = "Merchant";
                    break;
                case "327":
                    temp = "Boost";
                    type = "User";
                    break;

                case "346":
                    temp = "CImb";
                    type = "User";
                    break;

                case "329":
                    temp = "MCash";
                    type = "Merchant";
                    break;
                case "328":
                    temp = "MCash";
                    type = "User";
                    break;

                case "347":
                    temp = "GrabPay";
                    type = "User";
                    break;

                case "345":
                    temp = "MayBank";
                    type = "User";
                    break;

                case "348":
                    temp = "NETS";
                    type = "User";
                    break;

                case "291":
                    temp = "VCash";
                    type = "User";
                    break;

                case "338":
                    temp = "UnionPay";
                    type = "Merchant";
                    break;
                case "339":
                    temp = "UnionPay";
                    type = "User";
                    break;

                case "336":
                    temp = "Touch N GO";
                    type = "Merchant";
                    break;

                default:
                    temp = "SCAN";
                    type = "Scan";
                    break;
            }
            temp += "\n";
            equals = TextUtils.equals(temp, lastTemp);
            if (equals) {
                // TODO
            } else {
                printSeparateLine();
                printerService.printTextWithFont(temp, null, 20, null);
                printSeparateLine();
            }
            equals = TextUtils.equals("2", sumTotal.transTimes) || TextUtils.equals("3", sumTotal.transTimes);
            if (equals) {
                type += " VOID";
            } else {
                type += " SALE";
            }
            String[] texts = {
                    type,
                    sumTotal.transTimes,
                    transMoneyValue(sumTotal.sumMoney)
            };
            int[] widths = {14, 12, 13};
            int[] aligns = {0, 1, 1};
            printerService.printColumnsString(texts, widths, aligns, null);
            lastTemp = temp;
        }
        printSeparateLine();
    }

    private void printTitle(TotalSettlementDetail settlementDetail) throws Exception {
        printerService.setAlignment(1, null);

        Bitmap bitmap = getPrintTitleIcon(0);
        printerService.printBitmap(bitmap, null);
        printLine();

        String temp = getString(R.string.receipt_settlement_summary) + "\n";
        printerService.printTextWithFont(temp, null, 30, null);
        printerService.setAlignment(0, null);

        // 打印商户名称
        temp = getString(R.string.receipt_merchant_name) + IPay88Operation.getInstance().getIPay88Bean().merchantName + "\n";
        printerService.printTextWithFont(temp, null, 20, null);

        // merchant number
        temp = getString(R.string.receipt_merchant_number) + IPay88Operation.getInstance().getIPay88Bean().merchantCode + "\n";
        printerService.printTextWithFont(temp, null, 20, null);

        // terminal number
        temp = getString(R.string.receipt_terminal_number) + IPay88Operation.getInstance().getIPay88Bean().merchantID + "\n";
        printerService.printTextWithFont(temp, null, 20, null);

        // batch number
        // temp = getString(R.string.receipt_batch_number) + settlementDetail.batchNumber + "\n";
        // printerService.printTextWithFont(temp, null, 20, null);

        // date
        temp = getString(R.string.receipt_date) + settlementDetail.date + "\n";
        printerService.printTextWithFont(temp, null, 20, null);

        // time
        temp = getString(R.string.receipt_time) + settlementDetail.time + "\n";
        printerService.printTextWithFont(temp, null, 20, null);
    }

    private String transMoneyValue(String str) {
        return new BigDecimal(str).movePointLeft(2).toPlainString();
    }

}
